package dk.benches.webpages;

import static dk.benches.domain.Rating.RATING_VALUES;

import org.apache.wicket.markup.html.WebPage;
import org.apache.wicket.markup.html.form.Button;
import org.apache.wicket.markup.html.form.DropDownChoice;
import org.apache.wicket.markup.html.form.Form;
import org.apache.wicket.markup.html.form.TextArea;
import org.apache.wicket.markup.html.form.TextField;
import org.apache.wicket.markup.html.panel.FeedbackPanel;
import org.apache.wicket.model.CompoundPropertyModel;
import org.apache.wicket.model.PropertyModel;
import org.apache.wicket.spring.injection.annot.SpringBean;

import dk.benches.dao.BenchDao;
import dk.benches.domain.Bench;

public class CreateBench extends WebPage {
	@SpringBean private BenchDao benchDao;
	
	public CreateBench(){
		add(new FeedbackPanel("feedback"));
		add(createForm());
	}

/* Factory methods */
	
	protected Form createForm() {
		final Bench bench = new Bench();

		Form form = new Form("form", new CompoundPropertyModel(bench)){
			private static final long serialVersionUID = -6058595660936531649L;

			@Override
			protected void onSubmit() {
				super.onSubmit();
				
				/* Save bench to DB - exception handling!! */
				benchDao.createBench(bench);
								
				/* return to home-page */
				setResponsePage(HomePage.class);
			}
		};
		
		form.add(new TextField("country", new PropertyModel(bench, "location.country")).setRequired(true));
		form.add(new TextField("city", new PropertyModel(bench, "location.city")).setRequired(true));
		form.add(new TextField("street", new PropertyModel(bench, "location.street")));
		
		form.add(new DropDownChoice("locationRating", new PropertyModel(bench, "rating.locationRating"), RATING_VALUES).setRequired(true));
		form.add(new DropDownChoice("comfortRating", new PropertyModel(bench, "rating.comfortRating"), RATING_VALUES).setRequired(true));
		form.add(new DropDownChoice("facilitiesRating", new PropertyModel(bench, "rating.facilitiesRating"), RATING_VALUES).setRequired(true));		
		
		form.add(new TextArea("description").setRequired(true));
		
		form.add(new Button("save"));
		
		return form;
	}
}
